import unittest
from time import sleep
import  requests
from api.api_register_login import ApiRegisterLogin
from parameterized import parameterized
from util import read_json
from api import log

class TestRegisterLogin(unittest.TestCase):
    # 初始化
    def setUp(self) -> None:
            # 获取session对象
        self.session = requests.session()
        # 获取ApiRegisterLogin实例
        log.info("正在初始化session对象：{}".format(self.session))
        self.reg = ApiRegisterLogin(self.session)
    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()
        log.info("正在关闭session {}".format(self.session))
    @parameterized.expand(read_json("register_login.json", "img_code"))
    def test01_image_code(self, random, expect_code):
        try:
            i = self.reg.api_img_code(random)
            log.info("执行图片验证码响应状态码为：{}".format(i.status_code))
            self.assertEqual(expect_code, i.status_code)
            log.info("执行图片验证码断言通过")
        except Exception as e:
            log.error("断言失败，原因：{}".format(e))
            # 抛异常
            raise
    @parameterized.expand(read_json("register_login.json", "phone_code"))
    def test02_phone_code(self, phone, imgVerifyCode, expect_text):
        try:
            self.reg.api_img_code("123")
            i = self.reg.api_phone_code(phone=phone, imgVerifyCode=imgVerifyCode)
            log.info("执行接口结果为：{}".format(i.text))
            self.assertIn(expect_text, i.text)
            log.info("执行断言通过!")
        except Exception as e:
            log.error("断言失败，原因：{}".format(e))
            # 抛异常
            raise
    @parameterized.expand(read_json("register_login.json", "register"))
    def test03_register(self, phone, password, imgVerifyCode, phone_code, expect_text):
        try:
            self.reg.api_img_code("0.123")
            self.reg.api_phone_code(phone, imgVerifyCode)
            i = self.reg.api_register(phone, password, imgVerifyCode, phone_code)
            log.info("执行接口结果为：{}".format(i.text))
            self.assertIn(expect_text, i.text)
            log.info("执行断言通过!")
        except Exception as e:
            log.error("断言失败，原因：{}".format(e))
            # 抛异常
            raise
    @parameterized.expand(read_json("register_login.json", "login"))
    def test04_login(self, keywords, password, expect_text):
       try:
            a = 1
            if "error" in password:
                while a <= 3:
                    i = self.reg.api_login(keywords, password)
                    a += 1
                print("测试锁定：", i.text)
                self.assertIn("锁定", i.text)
                sleep(60)
                i = self.reg.api_login(keywords, "hao123")
                log.info("执行接口结果为：{}".format(i.text))
                self.assertIn(expect_text, i.text)
                log.info("执行断言通过!")
            else:
                i = self.reg.api_login(keywords, password)
                print(i.json())
                log.info("执行接口结果为：{}".format(i.text))
                self.assertIn(expect_text, i.text)
                log.info("执行断言通过!")
       except Exception as e:
           log.error("断言失败，原因：{}".format(e))
           # 抛异常
           raise

    # 5、查询登录状态接口 测试
    @parameterized.expand(read_json("register_login.json", "login_status"))
    def test05_login_status(self, status, expect_text):
            try:
                if status == "已登录":
                    # 调用登录接口
                    self.reg.api_login("18437015026", "hao123")
                    # 调用查询登录状态接口
                r = self.reg.api_login_status()
                print(r.json())
                log.info("执行接口结果为：{}".format(r.text))
                self.assertIn(expect_text, r.text)
                log.info("执行断言通过!")
            except Exception as e:
                log.error("断言失败，原因：{}".format(e))
                # 抛异常
                raise





